#include <standard.hlsli>
#include <WR2Parameters.hlsli>

struct PixelToFrame
{
	float4 Color;
};

PixelToFrame ps_main(VertexToPixel PSIn) : SV_TARGET
{
	PixelToFrame Output = (PixelToFrame)0;

	float4 Lay1 = xColoredTexture1_Tex.Sample(Tex1, PSIn.texCoord2.xy + (gametime * 0.1));

		float3 Ln = -lightDir;
		float3 Nn = Lay1.rgb;
		float3 In = normalize(PSIn.pos2.xyz - CameraPosition);// View direction
		float3 Hn = normalize(Ln - In);         // Half way between view & light

		float3 R = reflect(In, Nn);
		float3 Env = xColoredTexture4_Tex.Sample(Tex4, 0.5 - 0.5*R.xy).xyz*0.5;
		//float3 Env = texCUBE(Tex4, R);
		float ShadowCol = smoothstep(0.4375, 0.5625, 1.0 - PSIn.kBlend.w);
	float Spec = pow(max(0., dot(normalize(PSIn.normal), Hn)), 16.)*ShadowCol;

	Env = Env + Spec * pow(Lay1.a + 0.125, 2.);

	float fresnel = pow(1. + dot(Nn, In), 5.);
	float c = smoothstep(0.4375, 0.5625, PSIn.kBlend.b);

	Output.Color = float4(Env*(ShadowCol + PSIn.kColor.rgb), 0.25 + Lay1.a / 2.0)*c;

	Output.Color = float4(lerp(Output.Color.rgb, FogColor, PSIn.texCoord3.z), Output.Color.a);
	return  Output;
}